#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "shape.h"
#include "circle.h"
#include "square.h"
#include "line.h"
#include "panel.h"

int main() {
    int width, height;
    Panel panel;

    // 获取画板宽度和高度
    printf("请输入画板的宽度和高度（例如：40 20）：");
    scanf("%d %d", &width, &height);

    // 初始化画板
    initPanel(&panel, width, height);
	printPanel(&panel);

    int choice;
    do {
        // 显示操作菜单
        printf("\n请选择要绘制的图形:\n");
        printf("1. Circle (圆形)\n");
        printf("2. Square (方形)\n");
        printf("3. Line (直线)\n");
        printf("4. 退出\n");
        printf("请输入你的选择: ");
        scanf("%d", &choice);

        if (choice == 1) {
            // 绘制圆形
            int x, y, radius;
            char ch;
            printf("请输入圆心坐标 (x y): ");
            scanf("%d %d", &x, &y);
            printf("请输入圆的半径: ");
            scanf("%d", &radius);
            printf("请输入圆形的字符: ");
            scanf(" %c", &ch);  // 注意前面的空格，防止读取之前的换行符

            Circle circle = {{x, y, ch, drawCircle}, radius};
            circle.base.draw((Shape *)&circle, &panel);
        }
        else if (choice == 2) {
            // 绘制方形
            int x, y, size;
            char ch;
            printf("请输入方形左上角坐标 (x y): ");
            scanf("%d %d", &x, &y);
            printf("请输入方形的大小: ");
            scanf("%d", &size);
            printf("请输入方形的字符: ");
            scanf(" %c", &ch);

            Square square = {{x, y, ch, drawSquare}, size};
            square.base.draw((Shape *)&square, &panel);
        }
        else if (choice == 3) {
            // 绘制直线
            int x, y, length;
            char direction, ch;
            printf("请输入直线起点坐标 (x y): ");
            scanf("%d %d", &x, &y);
            printf("请输入直线长度: ");
            scanf("%d", &length);
            printf("请输入直线的方向 (H/V): ");
            scanf(" %c", &direction);
            printf("请输入直线的字符: ");
            scanf(" %c", &ch);

            Line line = {{x, y, ch, drawLine}, length, direction};
            line.base.draw((Shape *)&line, &panel);
        }
        else if (choice == 4) {
            printf("退出程序.\n");
			goto out;
		}
        else {
            printf("无效的选择，请重新输入.\n");
        }
		
		printPanel(&panel);

    } while (choice != 4);

out:
    // 释放画板内存
    freePanel(&panel);

    return 0;
}

